package com.zhongan.gateway.web.filter;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;

/**
 */
@Component
public class LoginFilter implements Filter {

    //    private Logger       logger       = LoggerFactory.getLogger(LoginFilter.class);
    private final static String COOKIE_NAME  = "name";
    private final static String COOKIE_VALUE = "21232f297a57a5a743894a0e4a801fc3";
    private final static int    COOKIE_AGE   = 0;
    private final static String PWD          = "1a31bcb08ffd0c890654ca6727c52868";

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
            ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        String uri = req.getRequestURI();
        //gw调用都过
        if (uri != null
                && (uri.startsWith("/gw") || uri.endsWith("login.html") || uri.endsWith(".css") || uri.endsWith(".js")
                        || uri.endsWith(".png") || uri.endsWith(".jpg") || uri.endsWith(".gif") || uri.endsWith(".ico") || uri
                            .endsWith(".woff2"))) {
            chain.doFilter(request, response);
            return;
        }

        //ajax 请求直接过
        if (null != req.getHeader("x-requested-with") && req.getHeader("x-requested-with").equals("XMLHttpRequest")) {
            chain.doFilter(request, response);
            return;
        }
        //校验cookie
        String name = getCookie(req, COOKIE_NAME);
        if (COOKIE_VALUE.equals(name)) {
            chain.doFilter(request, response);
        } else {
            //登录
            String pwd = req.getParameter("pwd");
            String loginName = req.getParameter("loginName");
            String md5Str = DigestUtils.md5Hex((loginName + "_" + pwd).getBytes("utf-8"));
            if (PWD.equals(md5Str)) {

                addCookie(resp, COOKIE_NAME, COOKIE_VALUE, COOKIE_AGE);
                resp.sendRedirect("/index");
            } else {
                resp.sendRedirect("/login.html");

            }
        }

    }

    private String getCookie(HttpServletRequest req, String name) {

        Cookie[] cookies = req.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                if (name.equals(cookie.getName())) {
                    return cookie.getValue();
                }

            }
        }
        return "";
    }

    private void addCookie(HttpServletResponse response, String name, String value, int maxAge) {
        Cookie cookie = new Cookie(name, value);
        cookie.setPath("/");
        if (maxAge > 0) {
            cookie.setMaxAge(maxAge);
        }
        response.addCookie(cookie);
    }

    @Override
    public void destroy() {

    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {

    }

}
